package net.sf.pulse.android;

import java.util.Map;
import java.util.TreeMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

public class ConfigureChannel extends Activity {
	
	private final Map<String, Integer> protocol2port = new TreeMap<String, Integer>();
	
	public ConfigureChannel() {

		protocol2port.put("DZ", 5000);
		protocol2port.put("xAP", 3639);
		protocol2port.put("xPL", 3865);
	}
	
	private Spinner spinnerProtocol;
	private EditText textSSID;
	private CheckBox checkboxSSID;
	private EditText textPort;

    @Override
    public void onCreate(Bundle savedInstanceState) {

    	super.onCreate(savedInstanceState);
        setContentView(R.layout.channel_configure);
        
        setupHostname();
        setupSpinner();
        setupSSID();
        setupButtons();
    }
    
    /**
     * Set up the hostname/IP address text input field callback.
     * 
     * The hostname/IP address text input field should spawn a background DNS lookup upon loss of focus.
     * 
     */
    private void setupHostname() {
    	
    }
    
    /**
     * Find the spinner, and stuff it with proper values.
     */
    private void setupSpinner() {
    	
    	// Need to do this before the spinner because the spinner handler will need the reference
    	
		textPort = (EditText)findViewById(R.id.channel_configure_port);

		spinnerProtocol = (Spinner) findViewById(R.id.spinner_configure_channel_protocol);
		
		// Hmm... The scope of this variable is really interesting
        final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.channel_protocols, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        spinnerProtocol.setAdapter(adapter);
        
		spinnerProtocol.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				CharSequence protocol = adapter.getItem((int)spinnerProtocol.getSelectedItemId());
				Integer port = protocol2port.get(protocol);
				textPort.setText(Integer.toString(port));
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    
    /**
     * Make sure that the checkbox is off, and SSID input field is disabled. 
     */
    private void setupSSID() {

    	checkboxSSID = (CheckBox)findViewById(R.id.channel_configure_wifi);
    	textSSID = (EditText)findViewById(R.id.channel_configure_ssid);
    	
    	checkboxSSID.setChecked(false);
    	textSSID.setEnabled(false);
    	
    	checkboxSSID.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        		textSSID.setEnabled(checkboxSSID.isChecked());
        	}
        });
    }
    
    private void setupButtons() {
    	
        Button buttonOK = (Button)findViewById(R.id.ok);
        Button buttonCancel = (Button)findViewById(R.id.cancel);
        
        buttonOK.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        		
        		// VT: FIXME: Need to read the fields, make sure they're OK
        		// and possibly forbid leaving the action if things don't match up
        		// (though this may not be the best idea if the host is known, but
        		// currently unavailable).
        		//
        		// If the result is accepted, clear the fields, otherwise leave them as is
        		// for the next attempt
        		
        	    setResult(RESULT_OK);
        	    finish();
        	}
        });

        buttonCancel.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View view) {
        	    setResult(RESULT_CANCELED);
        	    finish();
        	}
        });
    }

    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

}
